﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ClusterAggregation.GeneralTools
{
    using ClusterAggregation.Datum;
    using ClusterAggregation.DataSets;
    /**
     * This class enables one to write a dataset into a file.
     */
    public class CDatasetWriter
    {
        /**
         * writing a simple dataset to a file
         * @param filename (string) [IN] the name of the file we will write.
         * @param data (AData[]) [IN] the dataset we're going to write.
         */
        public static void writeDataToFile(string filename,ClusterAggregation.Datum.AData[] data)
        {
            TextWriter tw = new StreamWriter(filename);
            try
            {
                for (int i = 0; i < data.Length; i++)
                {
                    string str = "";
                    for (int j = 0; j < data[i].coordinates.Length; j++)
                    {
                        str += " " + data[i].coordinates[j];
                    }

                    tw.WriteLine(str);
                }
            }
            finally
            {
                tw.Close();
            }
        }

        /**
        * writing a clustered dataset to a file
        * @param filename (string) [IN] the name of the file we will write.
        * @param partition (partition) [IN] the dataset we're going to write.
        */
        public static bool writeParition(string filename, ClusterAggregation.DataSets.CPartition partition)
        {
            bool retval = true;
            try
            {
                TextWriter tw = new StreamWriter(filename);
                try
                {
                    tw.WriteLine(partition.name);
                    tw.WriteLine("" + partition.ANMI);

                    int k = partition.clusters.Count;
                    for (int i = 0; i < k; i++)
                    {
                        
                        if (partition.clusters[i].data.Count > 0)
                        {
                            tw.WriteLine("[");
                            foreach (AData datum in partition.clusters[i].data)
                            {
                                string str = "";
                                for (int j = 0; j < datum.coordinates.Length; j++)
                                {
                                    str += " " + datum.coordinates[j];
                                }

                                tw.WriteLine(str);
                            }
                            tw.WriteLine("]");
                        }
                    }
                }
                catch (Exception e)
                {
                    retval = false;
                }
                finally
                {
                    tw.Close();
                }
                return true;
            }
            catch (Exception e)
            {
                //System.Console.Write(e.Message);
                retval = false;
            }

            return retval;
        }
        public static void writeParitionToStream(Stream st, ClusterAggregation.DataSets.CPartition partition)
        {
            StreamWriter tw = new StreamWriter(st);
            tw.WriteLine(partition.name);
            tw.WriteLine("" + partition.ANMI);
            try
            { 
                int k = partition.clusters.Count;
                for (int i = 0; i < k; i++)
                {
                    tw.WriteLine("[");

                    foreach (AData datum in partition.clusters[i].data)
                    {
                        string str = "";
                        for (int j = 0; j < datum.coordinates.Length; j++)
                        {
                            str += " " + datum.coordinates[j];
                        }

                        tw.WriteLine(str);
                    }

                    tw.WriteLine("]");
                }
            }
            finally
            {
                tw.Close();
            }
        }
    }
}
